﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace SmartSpace.Silverlight
{
    public class SlApp : Application
    {
        private object _paramsLock = new object();
        private SlAppParams _params;

        public SlAppParams Params
        {
            get
            {
                if (_params == null)
                {
                    lock (_paramsLock)
                    {
                        if (_params == null)
                        {
                            _params = new SlAppParams();
                        }
                    }
                }
                return _params;
            }
        }

        public SlApp()
        {
            this.Startup += new StartupEventHandler(SlApp_Startup);
        }

        void SlApp_Startup(object sender, StartupEventArgs e)
        {
            if (e.InitParams.ContainsKey(SlAppParams._debugModelKey))
            {
                string value = e.InitParams[SlAppParams._debugModelKey];
                Params.DebugMode = Convert.ToBoolean(value);
            }
            else
            {
                Params.DebugMode = false;
            }

            if (e.InitParams.ContainsKey(SlAppParams._hostUrlKey))
            {
                string value = e.InitParams[SlAppParams._hostUrlKey];
                Params.HostUrl = new Uri(value, UriKind.Absolute);
            }
            else
            {
                Params.HostUrl = new Uri(Application.Current.Host.Source, "../../../");
            }

            if (e.InitParams.ContainsKey(SlAppParams._appUrlKey))
            {
                string value = e.InitParams[SlAppParams._appUrlKey];
                Params.AppUrl = new Uri(value, UriKind.Absolute);
            }
            else
            {
                Params.AppUrl = new Uri(Application.Current.Host.Source, "../");
            }
        }
    }

    public class SlAppParams
    {
        internal static string _debugModelKey = "DebugMode";
        internal static string _hostUrlKey = "HostUrl";
        internal static string _appUrlKey = "AppUrl";

        /// <summary>
        /// 是否处于debug模式
        /// </summary>
        public bool DebugMode
        {
            get
            {
                if (this._params.ContainsKey(_debugModelKey))
                {
                    return (bool)this._params[_debugModelKey];
                }
                else
                {
                    return false;
                }
            }
            set
            {
                if (this._params.ContainsKey(_debugModelKey))
                {
                    this._params[_debugModelKey] = value;
                }
                else
                {
                    this._params.Add(_debugModelKey, value);
                }
            }
        }

        public Uri HostUrl
        {
            get
            {
                if (this._params.ContainsKey(_hostUrlKey))
                {
                    return (Uri)this._params[_hostUrlKey];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (this._params.ContainsKey(_hostUrlKey))
                {
                    this._params[_hostUrlKey] = value;
                }
                else
                {
                    this._params.Add(_hostUrlKey, value);
                }
            }
        }

        public Uri AppUrl
        {
            get
            {
                if (this._params.ContainsKey(_appUrlKey))
                {
                    return (Uri)this._params[_appUrlKey];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (this._params.ContainsKey(_appUrlKey))
                {
                    this._params[_appUrlKey] = value;
                }
                else
                {
                    this._params.Add(_appUrlKey, value);
                }
            }
        }

        private Dictionary<string, object> _params;

        public SlAppParams()
        {
            _params = new Dictionary<string, object>();
        }

        public void Add(string key, object value)
        {
            if (!this._params.ContainsKey(key))
            {
                this._params.Add(key, value);
            }
            else
            {
                this._params[key] = value;
            }

        }
    }

    public enum SlAppParamsKeys
    {
        DebugMode = 1, 
    }
}
